package com.wwj.netty.bio.buffer;

import javax.naming.ldap.SortKey;
import java.io.*;
import java.net.Socket;

/**
 * Created by wuweijun on 2018/11/12.
 */
public class Client {
    private static Integer DEFAULT_SERVER_PORT=8080;
    private static String LOCAL_IP="127.0.0.1";

    public static void start(){
        System.out.println("客户端启动...");

        while (true) {
            Socket socket = null;
            BufferedWriter out = null;
            BufferedReader in = null;
            try {
                socket = new Socket(LOCAL_IP, DEFAULT_SERVER_PORT);
                out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                //客户端-请输入
                System.out.println("请输入：");
                String data = new BufferedReader(new InputStreamReader(System.in)).readLine();
                out.write(data);
                out.flush();

                String message = in.readLine();
                System.out.println("客户端收："+message);


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
